package cn.whdream.datastructure.recursion;

import javax.lang.model.element.Element;

/**
 * 
 * @author admin
 * 递归回溯给小球找路
 * 从 i，j的 (1,1)出发 ，走的6，5则走到了
 * 递归回溯 0没有走 1.已经走过了；不可以走 2是可以走 3.可以走，走不通
 * 策略 下右上左
 */
public class MingGong {
	public static void main(String[] args) {
		int[][] map = initMap();
		setWay(map, 1, 1);
		printf(map);
	}

	private static int[][] initMap() {
		int[][] map = new int[8][7];
		//使用1表示墙
		for (int i = 0; i < 7; i++) {
			map[0][i] = 1;
			map[7][i] = 1;
		}
		for(int i = 0; i< 8;i++) {
			map[i][0] = 1;
			map[i][6] = 1; 
		}
		map[3][1] = 1;
		map[3][2] = 1;
		
		map[2][2] = 1;
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < 7; j++) {
				System.out.print(map[i][j]+" ");
			}
			System.out.println();
		}
		return map;
	}
	public static void printf(int[][] map) {
		System.out.println();
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < 7; j++) {
				System.out.print(map[i][j]+" ");
			}
			System.out.println();
		}
	}
	public static boolean setWay(int[][]map ,int i,int j) {
		//设定跳出递归的条件
		if (map[6][4] == 2) {
			return true;
		}else {
			//为0则通过了
			if (map[i][j] == 0) {
				map[i][j] = 2;
				if (setWay(map, i+1, j)) {
					return true;
				}else if (setWay(map, i, j+1)) {
					return true;
				}else if (setWay(map, i-1, j)) {
					return true;
				}else if (setWay(map, i, j-1)) {
					return true;
				}else {
					map[i][j] = 3;
					return false;
				}
			}else {
				//map[i][j] == 1,2,3 走过了，或走不通
				return false;
			}
		}
	}
}
